package com.boot.app.development.controller;

import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.api.ApiResult;
import com.boot.app.development.domain.DevelopmentDoc;
import com.boot.app.development.service.IDevelopmentDocService;
import com.boot.app.development.vo.DocVO;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("development/doc")
public class DevelopmentDocController {

    private final IDevelopmentDocService service;

    @Autowired
    public DevelopmentDocController(
        IDevelopmentDocService service
    ){
        this.service = service;
    }

    @GetMapping("{id}")
    public ApiResult<DocVO> getDocVO(@PathVariable("id") long id){
        DocVO docVO = service.getDocVOById(id);
        return ApiResult.success(docVO);
    }

    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<DevelopmentDoc>> getDocPage(@PathVariable int page, @PathVariable int size, DevelopmentDoc doc){
        LambdaQueryWrapper<DevelopmentDoc> wrapper = new LambdaQueryWrapper<>();
        if(NumberUtil.isValidNumber(doc.getId())) wrapper.eq(DevelopmentDoc::getId, doc.getId());
        if(StringUtils.isNotBlank(doc.getName())) wrapper.like(DevelopmentDoc::getName, doc.getName());
        if(StringUtils.isNotBlank(doc.getType())) wrapper.eq(DevelopmentDoc::getType, doc.getType());
        Page<DevelopmentDoc> docPage = service.page(new Page<>(page, size), wrapper);
        return ApiResult.success(docPage);
    }

    @Operation(summary = "新增/修改用户")
    @PostMapping()
    public ApiResult<?> postOrPutDocVO(@RequestBody DocVO docVO){
        service.saveOrUpdateDocVO(docVO);
        return ApiResult.success();
    }

    @Operation(summary = "删除用户")
    @DeleteMapping("{id}")
    public ApiResult<?> deleteDocVO(@PathVariable("id") long id){
        service.removeDocById(id);
        return ApiResult.success();
    }

}
